<div class="panel panel-default panel-intro">
    <div class="panel-heading">
        <h3 class="panel-title">订单物料清单 - {$order.order_no}</h3>
        <div class="panel-actions">
            <a href="{:url('index')}" class="btn btn-default">返回订单列表</a>
        </div>
    </div>
    <div class="panel-body">
        <!-- 订单基本信息 -->
        <div class="row">
            <div class="col-md-12">
                <div class="alert alert-info">
                    <h4><i class="icon fa fa-info"></i> 订单信息</h4>
                    <p><strong>订单号：</strong>{$order.order_no} | 
                       <strong>客户：</strong>{$order.customer_name} | 
                       <strong>总数量：</strong>{$order.total_quantity}件 | 
                       <strong>交货期：</strong>
                       {php}
                           $deliveryTime = (int)$order['delivery_time'];
                           \think\Log::info("视图调试 - 转换后delivery_time值: " . $deliveryTime);
                           \think\Log::info("视图调试 - 转换后delivery_time类型: " . gettype($deliveryTime));
                           echo !empty($deliveryTime) && $deliveryTime > 0 ? date('Y-m-d', $deliveryTime) : (!empty($order['delivery_time']) ? $order['delivery_time'] : '未设置');
                       {/php}</p>
                </div>
            </div>
        </div>
        
        <!-- 物料需求统计 -->
        <div class="row">
            <div class="col-md-3">
                <div class="info-box">
                    <span class="info-box-icon bg-blue"><i class="fa fa-cubes"></i></span>
                    <div class="info-box-content">
                        <span class="info-box-text">物料种类</span>
                        <span class="info-box-number">{$orderMaterials|count}种</span>
                    </div>
                </div>
            </div>
            <div class="col-md-3">
                <div class="info-box">
                    <span class="info-box-icon bg-green"><i class="fa fa-money"></i></span>
                    <div class="info-box-content">
                        <span class="info-box-text">预估成本</span>
                        <span class="info-box-number">¥{$totalAmount}</span>
                    </div>
                </div>
            </div>
            <div class="col-md-3">
                <div class="info-box">
                    <span class="info-box-icon bg-yellow"><i class="fa fa-exclamation-triangle"></i></span>
                    <div class="info-box-content">
                        <span class="info-box-text">缺料种类</span>
                        <span class="info-box-number">{$shortageCount}种</span>
                    </div>
                </div>
            </div>
            <div class="col-md-3">
                <div class="info-box">
                    <span class="info-box-icon bg-red"><i class="fa fa-clock-o"></i></span>
                    <div class="info-box-content">
                        <span class="info-box-text">备料状态</span>
                        <span class="info-box-number">
                            {if condition="$shortageCount eq 0"}
                                <span class="text-success">正常</span>
                            {else}
                                <span class="text-danger">需采购</span>
                            {/if}
                        </span>
                    </div>
                </div>
            </div>
        </div>
        
        <!-- 物料需求清单 -->
        <div class="row">
            <div class="col-md-12">
                <div class="box">
                    <div class="box-header with-border">
                        <h3 class="box-title">物料需求清单</h3>
                        <div class="box-tools pull-right">
                            <button type="button" class="btn btn-success btn-sm" id="update-all-status">
                                <i class="fa fa-check"></i> 批量更新状态
                            </button>
                        </div>
                    </div>
                    <div class="box-body">
                        <table class="table table-bordered table-striped">
                            <thead>
                                <tr>
                                    <th width="5%">#</th>
                                    <th width="15%">物料名称</th>
                                    <th width="10%">规格型号</th>
                                    <th width="8%">单位</th>
                                    <th width="10%">需求数量</th>
                                    <th width="10%">当前库存</th>
                                    <th width="8%">库存状态</th>
                                    <th width="10%">推荐供应商</th>
                                    <th width="8%">预估单价</th>
                                    <th width="8%">预估金额</th>
                                    <th width="8%">采购状态</th>
                                </tr>
                            </thead>
                            <tbody>
                                {foreach name="orderMaterials" item="vo" key="k"}
                                <tr {if condition="$vo.material && $vo.required_quantity > $vo.material.stock"}class="danger"{/if}>
                                    <td>{$k+1}</td>
                                    <td>
                                        <strong>{$vo.material.name}</strong>
                                        {if condition="$vo.material.code"}
                                        <br><small class="text-muted">编码：{$vo.material.code}</small>
                                        {/if}
                                    </td>
                                    <td>{$vo.material.spec}</td>
                                    <td>{$vo.material.unit}</td>
                                    <td>
                                        <strong class="text-primary">{$vo.required_quantity}</strong>
                                        {if condition="isset($vo.loss_rate) && $vo.loss_rate gt 0"}
                                        <br><small class="text-muted">含损耗：{$vo.loss_rate}%</small>
                                        {/if}
                                    </td>
                                    <td>
                                        <span class="text-{if condition='$vo.material.stock lt $vo.required_quantity'}danger{else}success{/if}">
                                            {$vo.material.stock}
                                        </span>
                                    </td>
                                    <td>
                                        {if condition="$vo.material && $vo.required_quantity > $vo.material.stock"}
                                            <span class="label label-danger">缺货</span>
                                        {elseif condition="$vo.material && $vo.material.stock lt $vo.material.min_stock"}
                                            <span class="label label-warning">库存不足</span>
                                        {else}
                                            <span class="label label-success">正常</span>
                                        {/if}
                                    </td>
                                    <td>
                                        {if condition="$vo.supplier"}
                                            <strong>{$vo.supplier.name}</strong>
                                            {if condition="$vo.supplier.phone"}
                                            <br><small class="text-muted">{$vo.supplier.phone}</small>
                                            {/if}
                                        {else}
                                            <span class="text-muted">未设置</span>
                                        {/if}
                                    </td>
                                    <td>¥{$vo.estimated_price}</td>
                                    <td><strong class="text-success">¥{$vo.estimated_amount}</strong></td>
                                    <td>
                                        <select class="form-control purchase-status" data-id="{$vo.id}" data-field="purchase_status">
                                            <option value="0" {if condition="$vo.purchase_status eq 0"}selected{/if}>待采购</option>
                                            <option value="1" {if condition="$vo.purchase_status eq 1"}selected{/if}>已下单</option>
                                            <option value="2" {if condition="$vo.purchase_status eq 2"}selected{/if}>已到货</option>
                                        </select>
                                    </td>
                                </tr>
                                {/foreach}
                            </tbody>
                            <tfoot>
                                <tr class="success">
                                    <th colspan="9" class="text-right">物料成本总计：</th>
                                    <th><strong class="text-success">¥{$totalAmount}</strong></th>
                                    <th></th>
                                </tr>
                            </tfoot>
                        </table>
                    </div>
                </div>
            </div>
        </div>
        
        <!-- 缺料预警 -->
        {if condition="$shortageCount gt 0"}
        <div class="row">
            <div class="col-md-12">
                <div class="alert alert-danger">
                    <h4><i class="icon fa fa-warning"></i> 缺料预警</h4>
                    <p>以下物料库存不足，请及时采购：</p>
                    <ul>
                                {foreach name="orderMaterials" item="vo"}
                                {if condition="$vo.material && $vo.required_quantity > $vo.material.stock"}
                                <li><strong>{$vo.material.name}</strong> - 需求：{$vo.required_quantity} {$vo.material.unit}，库存：{$vo.material.stock} {$vo.material.unit}，缺货：{$vo.required_quantity - $vo.material.stock} {$vo.material.unit}</li>
                                {/if}
                                {/foreach}
                    </ul>
                </div>
            </div>
        </div>
        {/if}
        
        <!-- 采购建议 -->
        <div class="row">
            <div class="col-md-12">
                <div class="box">
                    <div class="box-header with-border">
                        <h3 class="box-title">采购建议</h3>
                    </div>
                    <div class="box-body">
                        <div class="row">
                            <div class="col-md-6">
                                <h5>需要采购的物料：</h5>
                                <table class="table table-condensed">
                                    <thead>
                                        <tr>
                                            <th>物料名称</th>
                                            <th>建议采购量</th>
                                            <th>推荐供应商</th>
                                            <th>预估成本</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                {foreach name="orderMaterials" item="vo"}
                                {if condition="$vo.material && $vo.required_quantity > $vo.material.stock"}
                                <tr>
                                    <td>{$vo.material.name}</td>
                                    <td>{$vo.required_quantity - $vo.material.stock} {$vo.material.unit}</td>
                                    <td>{$vo.supplier.name|default='未设置'}</td>
                                    <td>¥{$vo.estimated_price * ($vo.required_quantity - $vo.material.stock)}</td>
                                </tr>
                                {/if}
                                {/foreach}
                                    </tbody>
                                </table>
                            </div>
                            <div class="col-md-6">
                                <h5>采购提醒：</h5>
                                <ul class="list-unstyled">
                                    <li><i class="fa fa-check text-success"></i> 请及时联系供应商确认价格和交期</li>
                                    <li><i class="fa fa-check text-success"></i> 建议提前{$vo.material.lead_time|default='7'}天下单</li>
                                    <li><i class="fa fa-check text-success"></i> 到货后及时更新库存状态</li>
                                    <li><i class="fa fa-check text-success"></i> 物料齐备后方可开始生产</li>
                                </ul>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>